//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.liy.framework.web.spring.utils;

import com.liy.framework.common.utils.Tools;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

public class WebTools {
    public static String SESSION_USER_KEY = "_LOGINUSER";

    public WebTools() {
    }

    public static String getRequestIp(HttpServletRequest request) {
        String ip = request.getHeader("RequestIp");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }

        return ip;
    }

    public static String getProjectRealPath(HttpServletRequest request) {
        return request == null ? null : request.getSession().getServletContext().getRealPath("/");
    }

    public static Map getRequestParams(HttpServletRequest request) {
        Map map = new HashMap();
        Enumeration paramNames = request.getParameterNames();

        while(paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }

        return map;
    }

    public static String buildAllError2String(BindingResult bindingResult) {
        StringBuffer errorbuff = new StringBuffer();
        Iterator var2 = bindingResult.getFieldErrors().iterator();

        while(var2.hasNext()) {
            FieldError error = (FieldError)var2.next();
            errorbuff.append(error.getDefaultMessage()).append(";");
        }

        return errorbuff.toString();
    }

    public static String getRequesetAuthorization(HttpServletRequest request) {
        String authorization = request.getHeader("AuthKey");
        if (Tools.StringIsNullOrSpace(authorization)) {
            authorization = request.getParameter("AuthKey");
        }

        return authorization;
    }

    public static String getRequesetParameterOrHeader(HttpServletRequest request, String key) {
        String value = request.getHeader(key);
        if (Tools.StringIsNullOrSpace(value)) {
            value = request.getParameter(key);
        }

        return value;
    }

    public static String getRequesetJwtToken(HttpServletRequest request) {
        String authorization = request.getHeader("JwtToken");
        if (Tools.StringIsNullOrSpace(authorization)) {
            authorization = request.getParameter("JwtToken");
        }

        return authorization;
    }
}
